package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 目标：
 *
 * @author Ashely
 * @date 2024/6/11 11:55
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    @ApiOperation("新增菜品接口")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result add(@RequestBody DishDTO dishDTO) {
        dishService.add(dishDTO);
//        String key = "dish_"+dishDTO.getCategoryId();
//        cleanCache(key);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("菜品分页查询接口")
    public Result page(DishPageQueryDTO dishPageQueryDTO ) {
      PageResult pageResult = dishService.page(dishPageQueryDTO);
      return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("菜品批量删除接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
//        cleanCache("dish_*");
        return Result.success();
    }
    @GetMapping("/{id}")
        @ApiOperation("根据id查询菜品接口")
        public Result getById(@PathVariable Long id){
         DishVO dishVO = dishService.selectById(id);
         return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("修改菜品接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
//        cleanCache("dish_*");
        return Result.success();

    }
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id){
        dishService.startOrStop(status,id);
//        cleanCache("dish_*");
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品接口")
    public Result list(Long categoryId){
        List<Dish> dishList = dishService.list(categoryId);
        return Result.success(dishList);
    }
//    private void cleanCache(String pattern){
//        Set keys = redisTemplate.keys(pattern);
//        redisTemplate.delete(keys);
//
//    }

}
